﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Threading;

namespace Blitzcrank
{
    public enum ColorNames
    {
        Black = 0,
        Blue = 1,
        Green = 2,
        Red = 3,
        Yellow = 4,
        Orange = 5,
        Purple = 6,
        White = 7
    }

    public class Board
    {
        public int[,] board = new int[8,8];
        public IntPtr boardHandle;
        public bool gotPosition;
        public Point upperLeftCorner;

        public void SetUpperLeftPosition()
        {
            do
            {
                if (Win32.GetAsyncKeyState(Win32.VK_RETURN) != 0)
                {
                    upperLeftCorner = Mouse.GetPosition();
                    boardHandle = Win32.WindowFromPoint(Mouse.GetPosition());
                    while (Win32.GetAsyncKeyState(Win32.VK_RETURN) != 0)
                    {
                        Thread.Sleep(10);
                    }
                    gotPosition = true;
                }
                Thread.Sleep(10);
            } while (!gotPosition);
        }

        public ColorNames GetColorName(Color color)
        {
            ColorNames colorName = ColorNames.Black;
            if (color.R > 200 && color.G > 200 && color.B < 80)
            {
                colorName = ColorNames.Yellow;
            }
            else if (color.R > 200 && color.G > 200 && color.B < 150)
            {
                colorName = ColorNames.Orange;
            }
            else if (color.GetHue() > 290 && color.GetHue() < 310)
            {
                colorName = ColorNames.Purple;
            }
            else if (color.GetHue() < 10)
            {
                colorName = ColorNames.White;
            }
            else if (color.GetHue() > 340 && color.GetHue() < 360)
            {
                colorName = ColorNames.Red;
            }
            else if (color.GetHue() > 180 && color.GetHue() < 220)
            {
                colorName = ColorNames.Blue;
            }
            else if (color.GetHue() > 120 && color.GetHue() < 150)
            {
                colorName = ColorNames.Green;
            }

            return colorName;
        }

        public void GenerateBoard()
        {
            using (var boardImage = PixelColor.CaptureBoard(upperLeftCorner))
            {
                for (int y = 0; y < 8; y++)
                {
                    for (int x = 0; x < 8; x++)
                    {
                        using (var gemImage = new Bitmap(1, 1))
                        {
                            using (Graphics gemGraphics = Graphics.FromImage(gemImage))
                            {
                                gemGraphics.DrawImage(boardImage, 0, 0, new Rectangle(x*40, y*40, 3, 3),
                                                      GraphicsUnit.Pixel);
                                board[y, x] = (int) GetColorName(gemImage.GetPixel(0, 0));
                                gemGraphics.Dispose();
                            }
                            gemImage.Dispose();
                        }
                    }
                }
                boardImage.Dispose();
            }
        }
    }
}